package controller;

import dao.AdministratorPowerDao;
import dao.PowerDao;
import entity.Administrator;
import entity.Power;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pojo.tree;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/role")
public class AdministratorPower {
    @Autowired
    public AdministratorPowerDao administratorPowerDao;
    @Autowired
    public PowerDao powerDao;
    @RequestMapping("/PowerQuery")
   public ModelAndView query(HttpServletRequest request){
        ModelAndView model=new ModelAndView("index_show.jsp");
        //从Session中取出值
        int Aid=(int)request.getSession().getAttribute("Aid");
        //创建map集合
        Map<String,List<entity.AdministratorPower>> map=new HashMap<>();
        //根据Aid取出父级权限
        List<entity.AdministratorPower> list=administratorPowerDao.menu(Aid);
        //创建一个List集合给里面放父级中的子级
        List<entity.AdministratorPower> lists;
        for (entity.AdministratorPower ap:list){
            //根基权限父级名称取出该编号
            Power power=powerDao.PowerName(ap.getPower().getPowerName());
            entity.AdministratorPower admin=new entity.AdministratorPower();
            admin.setAid(Aid);
            admin.setPower(power);
            //根据取出的编号取出子级权限
            lists=administratorPowerDao.permission(admin);
            map.put(ap.getPower().getPowerName(),lists);
        }

        model.addObject("map",map);
        return model;
    }
    @RequestMapping("/showAdminPower")
    @ResponseBody
    public List<tree> showQuery(String aid){
        return administratorPowerDao.showQuery(Integer.parseInt(aid));
    }
}
